import React, { Component } from "react";
import { Redirect, Route } from "react-router-dom";

class AuthorizedRoute extends Component {
  render() {
    let userInfo = window.localStorage.getItem('userInfo')
    let { component: Component, ...rest } = this.props;

    //获取顶层provider上所有的信息
    return (
      <Route
        {...rest}
        render={ props => {
          //这里的<Component {...this.props} />实际上指向的是 目标页面组件
          return userInfo ? (
            <Component {...this.props} />
          ) : (
            <Redirect to={{ pathname: '/login', state: { from: props.location } }} />
          ); 
        }}
      />
    );
  }
 
}

export default AuthorizedRoute;

// const PrivateRoute = props => {
//   let userInfo = window.localStorage.getItem('userInfo')

//   if (userInfo) {
//     return <Fragment>{props.children}</Fragment>
//   } else {
//     return <Redirect to={{ pathname: '/login', state: { from: props.location } }} />
//   }
// }

// export default PrivateRoute
